package com.cats.activity;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.cats.R;
import com.cats.adapter.MemberAdapter;
import com.cats.utility.AccountManageUtility;

public class MemberActivity extends BaseActivity {

	private ListView listView = null;
	private MemberAdapter adapter = null;
	
	private ArrayList<String> arrayList = null;

	
	public MemberActivity() {

	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setActionBarTitle("Member");
		System.out.println("[MemberActivity]onCreate");	
		
		if (arrayList == null) {
			arrayList =	new ArrayList<String>();
			arrayList.add("My Addresses");
			arrayList.add("My Shipments");
		}
		
		adapter = new MemberAdapter(this, R.layout.member_listview_item, arrayList);
		listView = (ListView)findViewById(R.id.item_list);
		listView.setAdapter(adapter);	
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 0) { // my addresses
					System.out.println("[MemberActivity]Select:My Addresses");
					// navigate to addresses page
					final Intent intent = new Intent(MemberActivity.this, AddressListActivity.class);
					intent.putExtra("SOURCE", "com.cats.activity.MemberActivity");
				    startActivity(intent);					
				}
				else { // my shipments
					System.out.println("[MemberActivity]Select:My Shipments");
					// navigate to addresses page
					final Intent intent = new Intent(MemberActivity.this, ShipmentListActivity.class);
				    startActivity(intent);										
				}
			}
		});
	}
	
	private void showConfirmWithMsg(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						/**
						 * delete address and finish
						 */
			            AccountManageUtility.getSharedAccountManager(getApplicationContext()).doSignOut();
			            // navigate back to home
			            NavUtils.navigateUpFromSameTask(MemberActivity.this);
					}
				})
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						/**
						 * do nothing if cancel
						 */
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
        case R.id.action_signout:
        	this.showConfirmWithMsg("Are your sure you want to sign out?");
//            AccountManageUtility.getSharedAccountManager(getApplicationContext()).doSignOut();
//            // navigate back to home
//            NavUtils.navigateUpFromSameTask(this);
            return true;
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	/**
	 * init action bar
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.member_activity_actions, menu);
	    return super.onCreateOptionsMenu(menu);
	}


}
